package com.freework.sys.table.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.freework.freedbm.dao.TotalResult;
import com.freework.freedbm.exception.DAOException;
import com.freework.sys.permissions.external.exception.AppException;
import com.freework.sys.permissions.external.service.SessionInfo;
import com.freework.sys.table.dao.SysDicTableDAO;
import com.freework.sys.table.vo.SysDictableVO;
import com.freework.sys.table.vo.SysDictableCfg.field;
import com.freework.util.FormatUtil;
import com.freework.util.unmodifiableMap.KeysIndex;
import com.freework.util.unmodifiableMap.UnmodifiableKeyMap;

@Service("com.freework.sys.table.service.TableEditServiceImp")
public class TableEditServiceImp implements TableEditServiceI{
	@Resource()
	SysDicTableDAO dao=null;
	@Resource(name="dic.table.service.TableService")
	TableServiceI tableSerivce=null;
	
	
	public void setTableSerivce(TableServiceI tableSerivce) {
		this.tableSerivce = tableSerivce;
	}
	public void setDao(SysDicTableDAO dao) {
		this.dao = dao;
	}
	public boolean insertTable(SysDictableVO vo)throws DAOException{
		vo.setCreatetime(FormatUtil.stringDateTime2());
		Integer Userid=SessionInfo.getUser().getUserid();
		vo.setCreateuser(Userid);
		vo.setPredefine(0);
		boolean pand= dao.insertTable(vo);
		
		if(pand){
			
			try {
				tableSerivce.clearTableCache();
			} catch (AppException e) {
				e.printStackTrace();
				throw new DAOException(e);
			}
			

		}
		return pand;
	}
	public boolean updateTable(SysDictableVO vo)throws DAOException{
		boolean pand= dao.updateTable(vo);
		if(pand){
			try {
				tableSerivce.clearTableCache();
			} catch (AppException e) {
				e.printStackTrace();
				throw new DAOException(e);
			}

		}
		return pand;
		
	}
	 public TotalResult<SysDictableVO> queryPage(int start, int limit,SysDictableVO vo){
		 return dao.queryPage(start, limit, vo);
	 }
	/* (non-Javadoc)
	 * @see com.freework.sys.table.service.TableEditServiceI#find(com.freework.sys.table.vo.SysDictableVO)
	 */
	public List find(SysDictableVO vo) {
		List<SysDictableVO> tablelist= dao.query(vo,field.tableid,field.tablename,field.tablecnname);
	
		List list=new ArrayList(tablelist.size());
		UnmodifiableKeyMap map=null;
		KeysIndex keys=new KeysIndex("tableid","tablename","tablecnname");
		
		for (SysDictableVO sysDictableVO : tablelist) {
			map=new UnmodifiableKeyMap(keys);
			map.putIndex(0, sysDictableVO.getTableid());
			map.putIndex(1, sysDictableVO.getTablename());
			map.putIndex(2, sysDictableVO.getTablename()+"("+sysDictableVO.getTablecnname()+")");
			list.add(map);
		}
		
		return list;
		
	}
	 
	 
}
